package com.dytj.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dytj.pojo.User;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class UserDao {
	
	public static boolean addUser(User user) throws SQLException {
		Connection conn = (Connection) DBUtil.getConn();
//		Statement st = (Statement) conn.createStatement();
		String account = user.getAccount();
		String password = user.getPassword();
		String nick_name = user.getNickName();
		String gender = user.getGender();
		//sql处理
		String sql;
		if(nick_name==null) {
			sql = "insert into user (account,password,gender)value ('"+account+"','"+password+"','"+gender+"')";
		}else {
			sql = "insert into user (account,password,nick_name,gender)value ('"+account+"','"+password+"','"+nick_name+"','"+gender+"')";
		}
//		ResultSet rs = st.executeQuery(sql);
		
		PreparedStatement ps=conn.prepareStatement(sql);
		
		int count=ps.executeUpdate();
		if(count>0){
			ps.close();
			conn.close();  
			return true;
		}else{
			ps.close();
			conn.close();
			return false;
		}
	}
	
	public static List<User> userCheck(User user) throws SQLException{
		List<User> list = new ArrayList<User>();
		String accounts = user.getAccount();
		String password = user.getPassword();
		Connection conn = (Connection) DBUtil.getConn();
		Statement st = (Statement) conn.createStatement();
		String sql = "select * from user where accounts = '"+accounts+"' and  password = '" +password+"'";
		ResultSet rs = st.executeQuery(sql);
		while(rs.next()){
			User value = new User();
			value.setAccount(rs.getString("account"));
			value.setPassword(rs.getString("password"));
			value.setUserId(rs.getInt("user_id"));
			value.setGender(rs.getString("gender"));
			value.setNickName("nick_name");
			list.add(value);
		}
		rs.close();
		st.close();
		conn.close();
		return list;
	}
}
